package com.cloud.controller;

import com.cloud.entity.CommonResult;
import com.cloud.entity.Payment;
import com.cloud.service.IPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


/**
 * <h3>springcloud02</h3>
 * <p></p>
 *
 * @author : zhengyue
 * @date : 2020-06-02 09:02
 **/
@RestController
public class PaymentController {

    @Autowired
    private IPaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        boolean save = paymentService.save(payment);
        return save ? new CommonResult(200, "插入成功, serverPort: " + serverPort)
                : new CommonResult(500, "插入失败");
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult get(@PathVariable("id") Long id) {
        Payment payment = paymentService.getById(id);
        return payment != null ? new CommonResult(200, "查询成功, serverPort: " + serverPort, payment)
                : new CommonResult(500, "查询失败");

    }

    @GetMapping("/payment/feign/timeout")
    public String paymentFeignTimeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping("/payment/lb")
    public String getServerPort() {
        return serverPort;
    }

}
